﻿/******************************************************************
** 文 件 名: BD_ProjectAddInvestment_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-13 21:37:19
** 描    述: 项目贷款额度编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;


namespace Honestysoft.Financing.WebUI.Module.Project
{
	public partial class BD_ProjectAddInvestment_Edit : Page
	{	
		protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }
		
		private void BindControl()
		{
		}

        private void Bind()
        {
            //Honestysoft.Financing.BusinessLogic.Project bll = new BusinessLogic.Project(Globals.CurrentConnStringName);
            //BD_ProjectAddInvestment obj = null;
            //if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            //{
            //    obj = Utils.ProjectSession.Project.AddInvesments.Find((BD_ProjectAddInvestment find) => find.ID == Request.QueryString["ID"]);// new BaseBusinessLogic<BD_ProjectAddInvestment>().GetEntity(Request.QueryString["ID"]);
            //    //txtID.Text = obj.ID;
            //    hidProjectID.Value = obj.ProjectID;
            //    txtProjectID.Text = Utils.ProjectSession.Project.Name;// bll.GetProjectName(obj.ProjectID);
            //    numAmount.Text = obj.Amount.ToString("0.00");
            //    dtpAddDate.Text = obj.AddDate.ToString("yyyy-MM-dd");
            //    hidSponsor.Value = obj.Sponsor;
            //    //txtSponsor.Text = obj.Sponsor;
            //    txtSponsorName.Text = obj.SponsorName;
            //    dtpCreateTime.Text = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //    txtRemark.Text = obj.Remark;
            //    //chkIsConfirm.Checked = obj.IsConfirm;
            //}
            //else
            //{
            //    txtProjectID.Text = Utils.ProjectSession.Project.Name;// bll.GetProjectName(Request.QueryString["ProjectID"]);
            //    hidProjectID.Value = Utils.ProjectSession.Project.ID;// Request.QueryString["ProjectID"];
                hidSponsor.Value = UserLoginer.LoginUser.ID;
                txtSponsorName.Text = UserLoginer.LoginUser.Name;
                dtpCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //}
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            //{
            //    Edit();
            //}
            //else
            //{
                Add();
            //}
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {            
            try
            {
                //if (Utils.ProjectSession.Project.AddInvesments == null)
                //{
                //    Utils.ProjectSession.Project.AddInvesments = new System.Collections.Generic.List<BD_ProjectAddInvestment>();
                //}
                //Utils.ProjectSession.Project.AddInvesments.Insert(0, GetSubmitObject());
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.returnValue=true;window.close();", true);
                //BD_ProjectAddInvestment obj = GetSubmitObject();
                //int ret = new BaseBusinessLogic<BD_ProjectAddInvestment>().CreateEntity(obj);
                //if (ret == 1)
                //{
                //    ScriptHelper.Window.AlertClose("保存成功！", true);
                //}
                //else
                //{
                //    ScriptHelper.Window.Alert("保存失败！");
                //}
                BD_ProjectAddInvestment obj = GetSubmitObject();
                string json = JsonHelper.JsonSerializer<BD_ProjectAddInvestment>(obj);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.returnValue='" + Server.UrlEncode(json) + "';window.close();", true);
               
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        //private void Edit()
        //{
        //    try
        //    {
        //        BD_ProjectAddInvestment obj = GetSubmitObject();
        //        foreach (BD_ProjectAddInvestment item in Utils.ProjectSession.Project.AddInvesments)
        //        {
        //            if (item.ID == obj.ID)
        //            {
        //                item.AddDate = obj.AddDate;
        //                item.Amount = obj.Amount;
        //                item.Remark = obj.Remark;
        //                break;
        //            }
        //        }
        //        Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.returnValue=true;window.close();", true);
        //    }
        //    catch (BaseException bex)
        //    {
        //        ExceptionHelper.ExceptionDistribute(bex, false);
        //        ScriptHelper.Window.Alert(bex.Message);
        //    }
        //    catch (Exception ex)
        //    {
        //        ExceptionHelper.ExceptionDistribute(ex, false);
        //        ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
        //    }
        //}
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private BD_ProjectAddInvestment GetSubmitObject()
        {
            BD_ProjectAddInvestment obj = new BD_ProjectAddInvestment();
            //if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            //{
            //    obj.ID = Request.QueryString["ID"];
            //    obj.dataState = EnumItem.DataState.Modify;
            //}
            //else
            //{
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                obj.dataState = EnumItem.DataState.New;
            //}
            //obj.ProjectID = hidProjectID.Value;// txtProjectID.Text;
			if(numAmount.Text.Trim().Length > 0){obj.Amount = Convert.ToDecimal(numAmount.Text);}
			if(dtpAddDate.Text.Trim().Length > 0){obj.AddDate = Convert.ToDateTime(dtpAddDate.Text);}
            obj.Sponsor = hidSponsor.Value;// txtSponsor.Text;
            obj.SponsorName = Server.UrlEncode(txtSponsorName.Text);
			if(dtpCreateTime.Text.Trim().Length > 0){obj.CreateTime = Convert.ToDateTime(dtpCreateTime.Text);}
            obj.Remark = Server.UrlEncode(txtRemark.Text);
            obj.IsConfirm = true;// chkIsConfirm.Checked;
            return obj;
        }
	}
}